#include "volt_curr.h"
#include "adc.h"

uint16_t get_curr_value(void)
{
    uint16_t vdda = vdda_calibration();
    uint16_t iout,Vr0;
    uint16_t meam_vout, meam_vref;

    float KT1, t;
    KT1 = 20 * 4095 / vdda / 4;

    meam_vref = get_Vref_sampled_value();//Mean_Value_Filter(Vout_Buffer, ADC_SAMPLE_SIZE);
    meam_vout = get_Vout_sampled_value();//Mean_Value_Filter(Vref_Buffer, ADC_SAMPLE_SIZE);

    t = (meam_vout > meam_vref) ? (meam_vout - meam_vref) : (meam_vref - meam_vout);

    if(t < KT1)
        t = 0;
    Vr0 = t / 4095 * vdda ;
    // iout = Vr0 / R / 放大系数) -> Vr0 / (0.005 * 50) -> Vr0 *4
    iout = Vr0 * 4;
    return iout;
}
uint16_t get_vbus_value(void)
{
    uint16_t vdda = vdda_calibration();
    uint16_t V1, Vbus;
    uint16_t meam_Volt;
    // R1/(R1+R2) == V1 / Vbus --> Vbus = V1 * (R1+R2) / R1
    // V1 = meam_Volt_Buffer / 4095 * vdda
    meam_Volt = get_Volt_sampled_value();
    V1        = meam_Volt * vdda / 4095;
    Vbus      = V1 * (33 + 220) / 33;
    //0.9997x + 0.0351
    if(Vbus > 35)
    {
        Vbus = 0.9997 * Vbus + 35;
    }
    return Vbus;
}
